package com.nomito.minimito.service.impl;

import com.nomito.minimito.vo.RespAreaVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nomito.minimito.common.utils.PageUtils;
import com.nomito.minimito.common.utils.Query;

import com.nomito.minimito.dao.UmAreaDao;
import com.nomito.minimito.entity.UmAreaEntity;
import com.nomito.minimito.service.UmAreaService;


@Service("umAreaService")
public class UmAreaServiceImpl extends ServiceImpl<UmAreaDao, UmAreaEntity> implements UmAreaService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UmAreaEntity> page = this.page(
                new Query<UmAreaEntity>().getPage(params),
                new QueryWrapper<UmAreaEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<RespAreaVo> getAddressList() {
        //查出全部
        List<UmAreaEntity> list = this.list();
        List<RespAreaVo> collect = null;

        collect = list.stream().filter(v -> v.getParentId() == 0).map(item -> {
            RespAreaVo respAreaVo = new RespAreaVo();
            List<RespAreaVo.childrens> children = getChildren(list, item.getAreaId());
            respAreaVo.setChildren(children);
            respAreaVo.setLabel(item.getName());
            return respAreaVo;
        }).collect(Collectors.toList());
        return collect;
    }

    private List<RespAreaVo.childrens> getChildren(List<UmAreaEntity> selectList,int parentId){

        List<RespAreaVo.childrens> collect = selectList.stream().filter(v -> v.getParentId() == parentId).map(v -> {
            RespAreaVo.childrens children = new RespAreaVo.childrens(v.getName());
            return children;
        }).collect(Collectors.toList());
        return collect;
    }

}